package com.mondo.financialwallet.domain.interactor;

import com.mondo.financialwallet.domain.executor.PostExecutionThread;

import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * @param <R>       响应类型
 * @param <Params>  请求参数
 */
public abstract class UseCase <R, Params> {

    private final CompositeDisposable disposables;
    private final PostExecutionThread postExecutionThread;

    UseCase(PostExecutionThread postExecutionThread){
        this.disposables = new CompositeDisposable();
        this.postExecutionThread = postExecutionThread;
    }

    abstract Observable<R> buildUseCaseObservable(Params params);

    public void execute(DisposableObserver<R> observer, Params params){
        Observable<R> observable = buildUseCaseObservable(params)
                .subscribeOn(Schedulers.io())
                .observeOn(postExecutionThread.getScheduler());
        disposables.add(observable.subscribeWith(observer));
    }
}
